package com.haizhi.crm.common.util;

import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;

import java.security.MessageDigest;

public final class Md5SaltUtils {
    private static final GLog LOG = LogFactory.getLogger(Md5SaltUtils.class);
    private static final String SECRET_KEY = "&0123456789¥$ABCDEFGHI=+$%!~#@abcdefghijklmnopqrstuvwxyz";

    public static String byteToHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexString.append(hex.toUpperCase());
        }
        return hexString.toString();
    }

    public static String getEncrypted(String content) {
        byte[] salt = SECRET_KEY.getBytes();
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5" );
        } catch (Exception e) {
            LOG.error(e);
        }
        md.update(salt);
        md.update(content.getBytes());
        md.update(salt);
        byte[] digest = md.digest();
        return byteToHexString(digest);
    }
}